<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">编码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">应用</label>
                            <div class="layui-input-inline">
                                <select name="appCode" lay-search="">

                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">环境</label>
                            <div class="layui-input-inline">
                                <select name="envCode" lay-search="">

                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">数据源</label>
                            <div class="layui-input-inline">
                                <select name="datasourceCode" lay-search="">

                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">请求类型</label>
                            <div class="layui-input-inline">
                                <select name="reqType" lay-search="">
                                    <option value="">直接选择或搜索选择</option>
                                    <option value="json">json</option>
                                    <option value="xml">xml</option>
                                    <option value="queryString">queryString</option>
                                    <option value="formData">formData</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">响应类型</label>
                            <div class="layui-input-inline">
                                <select name="resType" lay-search="">
                                    <option value="">直接选择或搜索选择</option>
                                    <option value="json">json</option>
                                    <option value="xml">xml</option>
                                    <option value="file">file</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','common'], function () {
        var $ = layui.jquery,
            form = layui.form,
            common = layui.common,
            table = layui.table;
        var envHtmlText = '',appHtmlText = '',datasourceHtmlText = '',envData = [],appData = [],datasourceData = [];
        // 获取 app env datasource信息
        common.cmsAjax({
            url : '/baffleEnv/list',
            data : null,
            successFunc : function(response){
                var htmlText = '<option value="">直接选择或搜索选择</option>';
                $.each(response.data,function (i, n) {
                    htmlText += '<option value="'+n.code+'">'+n.name +'</option>';
                });
                envHtmlText = htmlText;
                envData = response.data;
                $(".table-search-fieldset").find("select[name='envCode']").html(htmlText);
                form.render();
            },
            errorFunc : function(response){
                layer.msg(response.msg || "获取环境配置失败", {icon : 2});
            }
        });common.cmsAjax({
            url : '/baffleApp/list',
            data : null,
            successFunc : function(response){
                var htmlText = '<option value="">直接选择或搜索选择</option>';
                $.each(response.data,function (i, n) {
                    htmlText += '<option value="'+n.code+'">'+n.name +'</option>';
                });
                appHtmlText = htmlText;
                appData = response.data;
                $(".table-search-fieldset").find("select[name='appCode']").html(htmlText);
                form.render();
            },
            errorFunc : function(response){
                layer.msg(response.msg || "获取应用配置失败", {icon : 2});
            }
        });common.cmsAjax({
            url : '/baffleDatasource/list',
            data : null,
            successFunc : function(response){
                var htmlText = '<option value="">直接选择或搜索选择</option>';
                $.each(response.data,function (i, n) {
                    htmlText += '<option value="'+n.code+'">'+n.name +'</option>';
                });
                datasourceHtmlText = htmlText;
                datasourceData = response.data;
                $(".table-search-fieldset").find("select[name='datasourceCode']").html(htmlText);
                form.render();
            },
            errorFunc : function(response){
                layer.msg(response.msg || "获取数据源配置失败", {icon : 2});
            }
        });

        table.render({
            elem: '#currentTableId',
            url: '/baffleInterface/selectPage',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 100, title: 'ID', sort: true},
                {field: 'name', width: 250, title: '名称'},
                {field: 'code', width: 250, title: '编码'},
                {field: 'uri', width: 400, title: '请求uri'},
                {field: 'reqType', width: 180, title: '请求类型'},
                {field: 'resType', width: 100, title: '响应类型'},
                {field: 'appCode', width: 180, title: '应用'},
                {field: 'envCode', width: 100, title: '坏境'},
                {field: 'datasourceCode', width: 180, title: '数据源'},
                {field: 'note', width: 400, title: '备注'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            page: true,
            method: 'post',
            loading: true,
            even: true,
            response : {
                statusCode : "success"
            },
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            for(var key in data.field){
                data.field[key] = $.trim(data.field[key]);
            }
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加接口',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/baffle/interface/add.html',
                    success: function(layero, indexOpen){
                        var contents =layer.getChildFrame('body',indexOpen).contents();
                        contents.find("select[name='envCode']").html(envHtmlText);
                        contents.find("select[name='appCode']").html(appHtmlText);
                        contents.find("select[name='datasourceCode']").html(datasourceHtmlText);
                        form.render();
                    }
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var codeList = [];
                $.each(data,function (i,n) {
                    codeList.push(n.id);
                });
                layer.confirm('真的批量删除行么', function (indexConfirm) {
                    layer.close(indexConfirm);
                    common.cmsAjax({
                        url : '/baffleInterface/del',
                        data : JSON.stringify(codeList),
                        successFunc : function(response){
                            var index = layer.alert("删除成功", {
                                title: '提示信息'
                            }, function () {
                                // 关闭弹出层
                                layer.close(index);
                                table.reload('currentTableId', {
                                    page: {
                                        curr: 1
                                    }
                                }, 'data');
                            });
                        },
                        errorFunc : function(response){
                            layer.msg(response.msg || "删除失败", {icon : 2});
                        }
                    });
                });
            } else if (obj.event === 'LAYTABLE_TIPS') {  // 监听删除操作
                layer.alert(
                    "该配置对接口做配置"
                );
            }

        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑接口',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/baffle/interface/edit.html',
                    success: function(layero, indexOpen){
                        var contents =layer.getChildFrame('body',indexOpen).contents();

                        var htmlText_app = '<option value="">直接选择或搜索选择</option>';
                        var htmlText_env = '<option value="">直接选择或搜索选择</option>';
                        var htmlText_datasource = '<option value="">直接选择或搜索选择</option>';
                        $.each(appData,function (i, n) {
                            htmlText_app += '<option value="'+n.code + '"';
                            if(data['appCode'] == n.code){//selected
                                htmlText_app += ' selected';
                            }
                            htmlText_app += '>'+n.name + '</option>';
                        });
                        $.each(envData,function (i, n) {
                            htmlText_env += '<option value="'+n.code + '"';
                            if(data['envCode'] == n.code){//selected
                                htmlText_env += ' selected';
                            }
                            htmlText_env += '>'+n.name +'</option>';
                        });
                        $.each(datasourceData,function (i, n) {
                            htmlText_datasource += '<option value="'+n.code + '"';
                            if(data['datasourceCode'] == n.code){//selected
                                htmlText_datasource += ' selected';
                            }
                            htmlText_datasource += '>'+n.name +'</option>';
                        });
                        contents.find("select[name='envCode']").html(htmlText_env);
                        contents.find("select[name='appCode']").html(htmlText_app);
                        contents.find("select[name='datasourceCode']").html(htmlText_datasource);
                        contents.find("input,select").each(function(){
                            var $this = $(this),
                                name = $this.attr("name");
                            $this.val(data[name] || "");
                        });
                        contents.find("textarea").each(function(){
                            var $this = $(this),
                                name = $this.attr("name");
                            $this.text(data[name] || "");
                        });
                        form.render();
                    }
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (indexConfirm) {
                    layer.close(indexConfirm);
                    common.cmsAjax({
                        url : '/baffleInterface/del',
                        data : JSON.stringify([data.id]),
                        successFunc : function(response){
                            var index = layer.alert("删除成功", {
                                title: '提示信息'
                            }, function () {
                                // 关闭弹出层
                                layer.close(index);
                                table.reload('currentTableId', {
                                    page: {
                                        curr: 1
                                    }
                                }, 'data');
                            });
                        },
                        errorFunc : function(response){
                            layer.msg(response.msg || "删除失败", {icon : 2});
                        }
                    });
                });
            }
        });


    });
</script>

</body>
</html>